
#include "MY_INC/APP/APP_ACLoad_Protection.h"
void Interrupt_Protection(InterruptController *controller);
static inline void Add_State_Trace(Monitor *sm, DeviceState state);

static inline void Add_State_Trace(Monitor *sm, DeviceState state)
{
    sm->state_trace[sm->state_trace_index++] = state;
    sm->state_trace_index %= STATE_TRACE_SIZE;
}
// 保护条件检查函数 (函数参数类型修正)
void Interrupt_Protection(InterruptController *controller)
{
    ProtectionState old_state = controller->monitor.protection_state; // 保存旧状态用于比较

    switch (controller->monitor.device_state)
    {
    case DEVICE_STATE_NORMAL: // 正常情况进行电压电流检测
    {
        controller->monitor.protection_state = PROT_STATE_NORMAL;
        controller->monitor.is_grid_over_voltage = Nonactivated; // 默认正常状态
        controller->monitor.is_bus_over_voltage = Nonactivated;
        controller->monitor.is_bus_under_voltage = Nonactivated;
        controller->monitor.is_inductor_over_current = Nonactivated;
        // 电网电压过压/欠压检测 (访问 param.vo_actual)
        if (fabsf(controller->param.vo_actual) > controller->monitor.grid_over_voltage_threshold)
        {
            controller->monitor.fault_detected = controller->monitor.is_grid_over_voltage = Activated;
            controller->monitor.protection_state = PROT_STATE_GRID_OVER_VOLTAGE;
        }
        // 母线电压事件处理 (访问 param.udc.actual)
        if ((controller->param.udc_actual > controller->monitor.bus_over_voltage_threshold))
        {
            controller->monitor.fault_detected = controller->monitor.is_bus_over_voltage = Activated;
            controller->monitor.protection_state = PROT_STATE_BUS_OVER_VOLTAGE;
        }
        else if ((controller->param.udc_actual < controller->monitor.bus_under_voltage_threshold))
        {
            controller->monitor.fault_detected = controller->monitor.is_bus_under_voltage = Activated;
            controller->monitor.protection_state = PROT_STATE_BUS_UNDER_VOLTAGE;
        }
        // 电感电流过流事件处理 (访问 param.il.actual)
        if (fabsf(controller->param.il_actual) > controller->monitor.il_over_current_threshold)
        {
            controller->monitor.fault_detected = controller->monitor.is_inductor_over_current = Activated;
            controller->monitor.protection_state = PROT_STATE_INDUCTOR_OVER_CURRENT;
        }

        // 多重故障检测 (访问 monitor 成员)
        controller->monitor.fault_count =
            controller->monitor.is_grid_over_voltage + controller->monitor.is_inductor_over_current +
            controller->monitor.is_bus_under_voltage + controller->monitor.is_bus_over_voltage;

        if (controller->monitor.fault_count >= 2) // 多重故障
        {
            controller->monitor.protection_state = PROT_STATE_MULTIPLE_FAULTS;
        }

        // 状态转移逻辑
        if (controller->monitor.fault_detected == Activated)
        {
            controller->monitor.device_state = DEVICE_STATE_HALT;
            break; // 跳出 switch 语句，进入故障处理逻辑
        }
        break;
    }
    case DEVICE_STATE_FAULT:
        break;
    case DEVICE_STATE_HALT: // 长期停机状态
    {
        // 使用95%阈值检查故障是否恢复 (访问 param 成员)
        controller->monitor.is_grid_over_voltage = (ActivatedState)(!(
            fabsf(controller->param.vo_actual) < 0.9f * controller->monitor.grid_over_voltage_threshold));
        // 这里的逻辑是反向的，因为在停机状态下，故障恢复意味着不再过压
        controller->monitor.is_inductor_over_current = (ActivatedState)(!(
            fabsf(controller->param.il_actual) < 0.9f * controller->monitor.il_over_current_threshold));

        controller->monitor.is_bus_over_voltage = (ActivatedState)(!(
            (controller->param.udc_actual < 0.9f * controller->monitor.bus_over_voltage_threshold)));

        controller->monitor.is_bus_under_voltage = (ActivatedState)(!(
            (controller->param.udc_actual > 1.1f * controller->monitor.bus_under_voltage_threshold)));
        // 所有故障清除
        if ((!controller->monitor.is_grid_over_voltage) & (!controller->monitor.is_inductor_over_current) &
            (!controller->monitor.is_bus_under_voltage) & (!controller->monitor.is_bus_over_voltage))
        {
            controller->monitor.fault_detected = Nonactivated;
            controller->monitor.device_state = DEVICE_STATE_NORMAL;
            controller->monitor.protection_state = PROT_STATE_NORMAL;
            break; // 跳出 switch 语句，进入正常状态处理逻辑
        }
        break;
    }
    default:
        controller->monitor.device_state = DEVICE_STATE_HALT;
        break;
    }
    // 记录状态变化（如果状态发生改变）
    if (old_state != controller->monitor.protection_state)
    {
        Add_State_Trace(&controller->monitor, controller->monitor.protection_state);
    }
}

// static inline void Interrupt_Protection(InterruptController *controller)
// {
//     ProtectionState old_state = controller->monitor.protection_state; // 保存旧状态用于比较

//     switch (controller->monitor.device_state)
//     {
//     case DEVICE_STATE_NORMAL: // 正常情况进行电压电流检测
//     {
//         controller->monitor.protection_state = PROT_STATE_NORMAL;
//         controller->monitor.is_grid_over_voltage = Nonactivated; // 默认正常状态
//         controller->monitor.is_bus_over_voltage = Nonactivated;
//         controller->monitor.is_bus_under_voltage = Nonactivated;
//         controller->monitor.is_inductor_over_current = Nonactivated;
//         // 电网电压过压/欠压检测 (访问 param.vo_actual)
//         if (fabsf(controller->param.vo_actual) > controller->monitor.grid_over_voltage_threshold)
//         {
//             controller->monitor.fault_detected = controller->monitor.is_grid_over_voltage = Activated;
//             controller->monitor.protection_state = PROT_STATE_GRID_OVER_VOLTAGE;
//         }
//         // 母线电压事件处理 (访问 param.udc.actual)
//         if ((controller->param.udc_actual > controller->monitor.bus_over_voltage_threshold))
//         {
//             controller->monitor.fault_detected = controller->monitor.is_bus_over_voltage = Activated;
//             controller->monitor.protection_state = PROT_STATE_BUS_OVER_VOLTAGE;
//         }
//         else if ((controller->param.udc_actual < controller->monitor.bus_under_voltage_threshold))
//         {
//             controller->monitor.fault_detected = controller->monitor.is_bus_under_voltage = Activated;
//             controller->monitor.protection_state = PROT_STATE_BUS_UNDER_VOLTAGE;
//         }
//         // 电感电流过流事件处理 (访问 param.il.actual)
//         if (fabsf(controller->param.il_actual) > controller->monitor.il_over_current_threshold)
//         {
//             controller->monitor.fault_detected = controller->monitor.is_inductor_over_current = Activated;
//             controller->monitor.protection_state = PROT_STATE_INDUCTOR_OVER_CURRENT;
//         }

//         // 多重故障检测 (访问 monitor 成员)
//         controller->monitor.fault_count =
//             controller->monitor.is_grid_over_voltage + controller->monitor.is_inductor_over_current +
//             controller->monitor.is_bus_under_voltage + controller->monitor.is_bus_over_voltage;

//         if (controller->monitor.fault_count >= 2) // 多重故障
//         {
//             controller->monitor.protection_state = PROT_STATE_MULTIPLE_FAULTS;
//         }

//         // 状态转移逻辑
//         if (controller->monitor.fault_detected == Activated)
//         {
//             // 检测到故障，立即关闭PWM
//             LL_GPIO_ResetOutputPin((GPIO_TypeDef *)controller->pwm.pwm_enable_gpio_port,
//                                    controller->pwm.pwm_enable_pin);
//             controller->pwm.pwm_gpio_flag = Nonactivated;
//             controller->monitor.fault_time++;
//             controller->monitor.device_state = DEVICE_STATE_FAULT;
//             break; // 跳出 switch 语句，进入故障处理逻辑
//         }
//         break;
//     }

//     case DEVICE_STATE_FAULT:
//     {
//         // 使用95%阈值检查故障是否恢复 (访问 param 成员)
//         controller->monitor.is_grid_over_voltage = (ActivatedState)(!(
//             fabsf(controller->param.vo_actual) < 0.95f * controller->monitor.grid_over_voltage_threshold));
//         // 这里的逻辑是反向的，因为在停机状态下，故障恢复意味着不再过压
//         controller->monitor.is_inductor_over_current = (ActivatedState)(!(
//             fabsf(controller->param.il_actual) < 0.95f * controller->monitor.il_over_current_threshold));

//         controller->monitor.is_bus_over_voltage = (ActivatedState)(!(
//             (controller->param.udc_actual < 0.95f * controller->monitor.bus_over_voltage_threshold)));

//         controller->monitor.is_bus_under_voltage = (ActivatedState)(!(
//             (controller->param.udc_actual > 1.05f * controller->monitor.bus_under_voltage_threshold)));

//         // 所有故障都清除
//         if ((!controller->monitor.is_grid_over_voltage) & (!controller->monitor.is_inductor_over_current) &
//             (!controller->monitor.is_bus_under_voltage) & (!controller->monitor.is_bus_over_voltage))
//         {
//             controller->monitor.fault_detected = Nonactivated;
//             LL_GPIO_SetOutputPin((GPIO_TypeDef *)controller->pwm.pwm_enable_gpio_port,
//             controller->pwm.pwm_enable_pin); controller->pwm.pwm_gpio_flag = Activated;
//             controller->monitor.fault_time = 0;
//             controller->monitor.protection_state = PROT_STATE_NORMAL;
//             controller->monitor.device_state = DEVICE_STATE_NORMAL;
//             break;
//         }
//         // 故障持续，检查是否需要停机
//         if (++controller->monitor.fault_time > 5)
//         {
//             LL_GPIO_ResetOutputPin((GPIO_TypeDef *)controller->pwm.pwm_enable_gpio_port,
//                                    controller->pwm.pwm_enable_pin); // 关闭PWM总闸
//             controller->pwm.pwm_gpio_flag = Nonactivated;
//             controller->loop_sm.stop_flag = Activated; // 对应旧的 loop_state_machine.Stop
//             controller->monitor.fault_count = 0;
//             controller->monitor.fault_time = 0;
//             controller->monitor.device_state = DEVICE_STATE_HALT;
//             break; // 跳出 switch 语句，进入停机状态处理逻辑
//         }
//         break;
//     }

//     case DEVICE_STATE_HALT: // 长期停机状态
//     {
//         // 使用95%阈值检查故障是否恢复 (访问 param 成员)
//         controller->monitor.is_grid_over_voltage = (ActivatedState)(!(
//             fabsf(controller->param.vo_actual) < 0.95f * controller->monitor.grid_over_voltage_threshold));
//         // 这里的逻辑是反向的，因为在停机状态下，故障恢复意味着不再过压
//         controller->monitor.is_inductor_over_current = (ActivatedState)(!(
//             fabsf(controller->param.il_actual) < 0.95f * controller->monitor.il_over_current_threshold));

//         controller->monitor.is_bus_over_voltage = (ActivatedState)(!(
//             (controller->param.udc_actual < 0.95f * controller->monitor.bus_over_voltage_threshold)));

//         controller->monitor.is_bus_under_voltage = (ActivatedState)(!(
//             (controller->param.udc_actual > 1.05f * controller->monitor.bus_under_voltage_threshold)));
//         // 所有故障清除
//         if ((!controller->monitor.is_grid_over_voltage) & (!controller->monitor.is_inductor_over_current) &
//             (!controller->monitor.is_bus_under_voltage) & (!controller->monitor.is_bus_over_voltage))
//         {
//             LL_GPIO_SetOutputPin((GPIO_TypeDef *)controller->pwm.pwm_enable_gpio_port,
//                                  controller->pwm.pwm_enable_pin); // 恢复PWM总闸
//             controller->pwm.pwm_gpio_flag = Activated;
//             controller->monitor.fault_detected = Nonactivated;
//             controller->monitor.device_state = DEVICE_STATE_NORMAL;
//             controller->monitor.protection_state = PROT_STATE_NORMAL;
//             break; // 跳出 switch 语句，进入正常状态处理逻辑
//         }
//         break;
//     }
//     default:
//         controller->monitor.device_state = DEVICE_STATE_HALT;
//         break;
//     }
//     // 记录状态变化（如果状态发生改变）
//     if (old_state != controller->monitor.protection_state)
//     {
//         Add_State_Trace(&controller->monitor, controller->monitor.protection_state);
//     }
// }

